package com.guanghua.brick.html.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.html.IComponent;

public class ContainTableTag extends BodyTagSupport implements IComponent {
	private static Log logger = LogFactory.getLog(ContainTableTag.class);
	
	private String id = null;
	private String name = null;
	private String html = "";
	private String action = null;
	private String prefix = null;

	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			out.println(buildForm());
		} catch (IOException e) {
			logger.error("io exception on print table html", e);
		}
		return BodyTagSupport.EVAL_PAGE;
	}
	
	private String buildForm() {
		StringBuffer html = new StringBuffer();
		html.append("<table id=\"")
			.append(this.id)
			.append("\" name=\"")
			.append(this.name)
			.append("\" ");
		
		if (this.action != null)
			html.append("action=\"")
				.append(WebAppContextTag.getWebAppContext(pageContext) + this.action)
				.append("\" ");
		if (this.prefix != null)
			html.append("parameterPrefix=\"")
				.append(this.prefix)
				.append("\" ");
				
		html.append(this.html)
			.append(">\n");
		
		html.append(bodyContent.getString());
		html.append("</table>\n");

		html.append("<script language=\"javascript\">\n")
			.append("	var ")
			.append(this.id)
			.append(" = new Table(\"")
			.append(this.id)
			.append("\");\n")
			.append("</script>\n");
		return html.toString();
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
	
	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
}
